<?php

namespace app\model;

use app\utils\Md5Service;
use \think\facade\Db;
use think\facade\Log;

class GpsInfoModel
{
    public static function getInfoByCarNumber($number)
    {
        $data = Db::name('gpsinfo')->where('license_plate', $number)->field('longitude,latitude,direction,speed,add_time,equi_identity,license_plate')->order('id', 'desc')->find();
        if (!empty($data)) {
            $carId = Db::table('xl_cars')->where('equi_identity', $data['equi_identity'])->value('id');
            $type = self::comTime();
            $where[] = ['nt.task_type', '=', $type];
            $where[] = ['nt.car_id', '=', $carId];
            $where[] = ['nt.task_status ', 'in', [2, 3]];
            $where[] = ['nt.date', '=', date("Y-m-d", time())];
            $taskData = Db::table('xl_normal_task')->alias('nt')->where([$where])
                ->field('nt.no,nt.car_id,driver.name as driver_name,li.line_no as line_no,cars.equi_identity,cars.car_plate_number')
                ->join('xl_cars cars', 'cars.id = nt.car_id', 'left')
                ->join('xl_driver driver', 'driver.id = nt.driver_id', 'left')
                ->join('xl_parnter_line li', ' li.id  = nt.line_id', 'left')
                ->find();
            if (!empty($taskData)) {
                $data['worker_status'] = 1;
                $data['line_no'] = $taskData['line_no'];
                $data['driver_name'] = $taskData['driver_name'];
                $data['task_no'] = $taskData['no'];
            } else {
                $data['worker_status'] = 0;
                $data['line_no'] = "";
                $data['driver_name'] = "";
                $data['task_no'] = "";
            }
        } else {
            $data = "";
        }
        $networkList = Db::table('xl_networks')->where('status', 1)->field('id,name,bd_gps_longitude,bd_gps_dimension')->select()->toArray();
        $allData = ['car_data' => $data, 'network_data' => $networkList];
        return $allData;
    }

    public static function getAllInfoLocation_bac()
    {
        $carIdentity = Db::table('xl_cars')->where('status', 1)->column('equi_identity');
        $data = [];
        foreach ($carIdentity as $v) {
            $curentData = Db::table('xl_gpsinfo')->where('equi_identity', $v)->field('longitude,latitude,direction,speed,add_time')->order('id', 'desc')->find();
            array_push($data, $curentData);
        }
        return $data;
    }

    public static function getAllInfoLocation()
    {
        //查询车辆最后一次出现的位置信息
        $carIdentity = Db::table('xl_cars')->where('status', 1)->column('equi_identity');
        $data = [];
        foreach ($carIdentity as $v) {
            $curentData = Db::table('xl_gpsinfo')->where('equi_identity', $v)->field('longitude,latitude,direction,speed,add_time,equi_identity,license_plate')->order('id', 'desc')->find();
            if (!empty($curentData)) {
                $data[] = $curentData;
            }
        }
        $carIds = Db::table('xl_cars')->where('equi_identity', 'in', $carIdentity)->column('id');


        $type = self::comTime();
        $where[] = ['nt.task_type', '=', $type];
        $where[] = ['nt.car_id', 'in', $carIds];
        $where[] = ['nt.task_status ', 'in', [2, 3]];
        $where[] = ['nt.date', '=', date("Y-m-d", time())];
        $taskData = Db::table('xl_normal_task')->alias('nt')->where([$where])
            ->field('nt.no,nt.car_id,driver.name as driver_name,li.line_no as line_no,cars.equi_identity,cars.car_plate_number')
            ->join('xl_cars cars', 'cars.id = nt.car_id', 'left')
            ->join('xl_driver driver', 'driver.id = nt.driver_id', 'left')
            ->join('xl_parnter_line li', ' li.id  = nt.line_id', 'left')
            ->select()->toArray();
        $newArr = [];
        foreach ($taskData as $k => $v) {
            $newArr[$v['car_plate_number']] = $v;
        }
        foreach ($data as $k => $v) {
            if (!empty($newArr[$v['license_plate']])) {
                $data[$k]['worker_status'] = 1;
                $data[$k]['line_no'] = $newArr[$v['license_plate']]['line_no'];
                $data[$k]['driver_name'] = $newArr[$v['license_plate']]['driver_name'];
                $data[$k]['task_no'] = $newArr[$v['license_plate']]['no'];
            } else {
                $data[$k]['worker_status'] = 0;
                $data[$k]['line_no'] = "";
                $data[$k]['driver_name'] = "";
                $data[$k]['task'] = "";
            }
        }
        $networkList = Db::table('xl_networks')->where('status', 1)->field('id,name,bd_gps_longitude,bd_gps_dimension')->select()->toArray();
        $allData = ['car_data' => $data, 'network_data' => $networkList];
        return $allData;
    }

    public static function comTime()
    {
        $nowTIme = time();
        $targetTime = strtotime(date("Y-m-d") . " 14:30:00");
        if ($nowTIme > $targetTime) {
            return 2;
        } else {
            return 1;
        }
    }
}
